package com.slz3.business.frame.util;

import org.springframework.cglib.core.ReflectUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @FileName: BeanMapUtils
 * @Description: bean2map map2bean工具类
 */


public class BeanMapUtils {


    public static void convert(Map map, Object obj) {
        if (map != null)
            Arrays.stream(ReflectUtils.getBeanProperties(obj.getClass())).forEach(propertyDescriptor -> {
                try {
                    Object value = map.get(propertyDescriptor.getName());
                    if (value != null && value.toString().length() != 0) {
                        String fieldType = propertyDescriptor.getPropertyType().getTypeName();
                        if (String.class.getTypeName().equals(fieldType)) {
                            propertyDescriptor.getWriteMethod().invoke(obj, value.toString());
                        } else if (Long.class.getTypeName().equals(fieldType)) {
                            propertyDescriptor.getWriteMethod().invoke(obj, Long.valueOf(value.toString()));
                        } else if (BigDecimal.class.getTypeName().equals(fieldType)) {
                            propertyDescriptor.getWriteMethod().invoke(obj, new BigDecimal(value.toString()));
                        } else if (List.class.getTypeName().equals(fieldType)) {
                            propertyDescriptor.getWriteMethod().invoke(obj, Arrays.asList(value));
                        } else if (Integer.class.getTypeName().equals(fieldType)) {
                            propertyDescriptor.getWriteMethod().invoke(obj, Integer.valueOf(value.toString()));
                        } else {
                            propertyDescriptor.getWriteMethod().invoke(obj, value);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            });
    }

    /**
     * 实体对象转成Map
     * @param obj 实体对象
     * @return
     */
    public static Map<String, Object> object2Map(Object obj) {
        Map<String,Object> reMap = new HashMap<String,Object>();
        if (obj == null)
            return null;
        try {
            Class<?> objClass = obj.getClass();
            while(objClass != null){
                Field[] fields = objClass.getDeclaredFields();
                for(int i=0;i<fields.length;i++){
                    try {
                        Field f = objClass.getDeclaredField(fields[i].getName());
                        f.setAccessible(true);
                        Object o = f.get(obj);
                        reMap.put(fields[i].getName(),VE.isNotEmpty(o)?o.toString():o);
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                objClass = objClass.getSuperclass();
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return reMap;
    }

    /**
     * Map转成实体对象
     * @param map map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     */
    public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
        if (map == null) {
            return null;
        }
        Object obj = null;
        try {
            obj = clazz.newInstance();

            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}